clear all
version 11
set more off

global root "~\APEEP_2008\01_results"
cd $root

use .\Stata_working\data_msa_fips_2010, clear
gen flag_msa=1
merge 1:1 fips using .\Stata_working\data_md_county_average 

/*
74 counties belonging to MSA, but not in AP calculation.
1,095 counties are MSA and in AP calculation.
3109 = 1095+2014.

    Result                           # of obs.
    -----------------------------------------
    not matched                         2,088
        from master                        74  (_merge==1)
        from using                      2,014  (_merge==2)

    matched                             1,095  (_merge==3)
    -----------------------------------------
*/

drop if _merge==1
replace flag_msa=0 if flag_msa==.
tab _merge flag_msa

/*
                      |       flag_msa
               _merge |         0          1 |     Total
----------------------+----------------------+----------
       using only (2) |     2,014          0 |     2,014 
          matched (3) |         0      1,095 |     1,095 
----------------------+----------------------+----------
                Total |     2,014      1,095 |     3,109 
*/

drop _merge
sort fips

compress
saveold .\Stata_working\data_2008_final_msa, /*version(11)*/ replace
outsheet using .\Stata_working\data_2008_final_msa.csv, comma nolabel replace

* obtain emission-weighted MD
use .\Stata_working\data_2008_final_msa, clear
collapse (sum) e_nh3- d_voc, by(flag_msa)

local pollutant nh3 pm25 nox so2 voc
foreach pol of local pollutant {
	gen md_`pol' = d_`pol'/e_`pol'
}

outsheet using .\Stata_working\results_2008_final_msa.csv, comma nolabel replace



